#include <QtGui>
#include "GenEditor.h"
#include "GenEditor.h"
#include "Settings.h"
#include "GenHighlighter.h"
#include "gen2/Parser.h"
#include "StringTools.h"

GenEditor::GenEditor(void)
{
	new GenHighlighter(document());
	document()->setModified(false);
}


GenEditor::~GenEditor(void)
{
}

bool GenEditor::dirty()
{
	return document()->isModified();
}

void GenEditor::setDirty(bool dirty)
{
	document()->setModified(dirty);
}

const QString& GenEditor::fileFilter()
{
	static const QString ext = "gen2 Model file (*.gen2m)";
	return ext;
}

void GenEditor::clear()
{
	setPlainText(QString());
}

bool GenEditor::save(const QString& path)
{
	QFile file(path); // Write the text to a file
	if (file.open(QIODevice::WriteOnly))
	{
		QTextStream stream(&file);
		stream << toPlainText();
		document()->setModified(false);
		return true;
	}
	return false;
}

bool GenEditor::load(const QString& path)
{
	QFile file(path); // Read the text from a file
	if (file.open(QIODevice::ReadOnly))
	{
		QTextStream stream(&file);
		setPlainText(stream.readAll());
		document()->setModified(false);
		return true;
	}
	return false;
}

SemanticModel::ModelPtr GenEditor::generateModel()
{
	std::wstring source = toWString(toPlainText());
	SemanticModel::ModelPtr model = GenParser::parse(source);
	return model;
}

void GenEditor::setModel(const SemanticModel::Model& m)
{
}
